<?php
namespace app\admin\controller;


//use app\admin\service\Common as CommonService;
//use app\admin\service\Task as TaskService;
use app\admin\service\Settle as SettleService;

class Settle extends Base
{
    protected $sTask;

    protected $sCommon;

    protected $sSettle;

    public function __construct()
    {
        parent::__construct();
        //$this->sTask = new TaskService();
        //$this->sCommon = new CommonService();
        $this->sSettle = new SettleService();
    }


    public function index(){
        $list = $this->sSettle->getSettleListByType(1, []);
        //var_dump($list);
        $capital_type = config('dic.capital_type');
        $capital_status = config('dic.capital_status');

        $this->assign('capital_type', $capital_type);
        $this->assign('capital_status', $capital_status);

        $this->assign('list', $list);
        return $this->fetch('settle_list');
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 10:06
     * @description 待审核任务单
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function waitSettle()
    {
        $param['user'] = input('user');
        $param['date'] = input('date');
        $param['page'] = input('page', 1);
        $param['status'] = input('status');
        $task_list = $this->sSettle->getSettleListByType(1, $param);
        return $this->apiSuccess('success', $task_list);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 10:06
     * @description 所有任务单
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allSettle()
    {
        $param['user'] = input('user');
        $param['date'] = input('date');
        $param['page'] = input('page', 1);
        $param['status'] = input('status');
        $task_list = $this->sSettle->getSettleListByType(2, $param);
        return $this->apiSuccess('success', $task_list);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 11:40
     * @description 获取结算单详情
     * @return \think\Collection|\think\model\Collection
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function settleDetail()
    {
        $settle_id = input('settle_id');
        if (!$settle_id) {
            return $this->apiError('请求失败,请重试');
        }
        $settle = $this->sSettle->getSettleInfo($settle_id);
        if (empty($settle)) {
            return $this->apiError('获取结算单失败,请重试');
        } else {
            $task = $this->sSettle->getSettleTaskInfo($settle['task_id'], $settle['work_id']);
            if (empty($task)) {
                return $this->apiError('获取结算单失败,请重试');
            } else {
                $settle['task'] = $task;
                return $this->apiSuccess('success', $settle);
            }
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 14:22
     * @description 审核结算单
     * @return \think\Collection|\think\model\Collection
     */
    public function auditSettle()
    {
        $settle_id = input('settle_id');
        if (!$settle_id) {
            return $this->apiError('请求失败,请重试');
        }
        $type = input('type', 1);
        $remark = input('remark', '');
        if ($type == 1) {
            $res = $this->sSettle->settleAuditCheckPass($settle_id, $remark);
            switch ($res) {
                case 1:
                    return $this->apiSuccess("审核通过任务单成功");
                    break;
                case -10:
                    return $this->apiError("权限不正确,请重试");
                    break;
                case -11:
                    return $this->apiError("提现用户账号信息不正确,请重试");
                    break;
                case -12:
                    return $this->apiError("提现用户未完善银行账户信息,请重试");
                    break;
                default:
                    return $this->apiError("审核通过任务单失败,请重试");
                    break;
            }
        } else {
            $res = $this->sSettle->settleAuditCheckRefuse($settle_id, $remark);
            switch ($res) {
                case 1:
                    return $this->apiSuccess('审核退回任务单成功', $res);
                    break;
                default:
                    return $this->apiError("审核退回任务单失败,请重试");
                    break;
            }
        }
    }
}